| Conditions | 21 |
| Paths | 60 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like page.js ➔ page often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import mkdirp from 'mkdirp' |
||
| 13 | var page = function (req, res, next) { |
||
| 14 | var filePath = cleanSlug(req.query.filePath) |
||
| 15 | filePath = fileUtils.getFilePath(filePath) |
||
| 16 | var html = (req.query.html) ? true : false |
||
| 17 | var json = null |
||
| 18 | var editor = false |
||
| 19 | if(typeof req.body.json !== 'undefined' && req.body.json !== null) { |
||
| 20 | editor = true |
||
| 21 | if(typeof req.body.json === 'string') { |
||
| 22 | json = JSON.parse(req.body.json) |
||
| 23 | }else { |
||
| 24 | json = req.body.json |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | if(typeof filePath !== 'undefined' && filePath !== null) { |
||
| 29 | var jsonPath = null |
||
| 30 | var linkPath = null |
||
| 31 | |||
| 32 | var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath) |
||
| 33 | if(typeof filePathTest !== 'undefined' && filePathTest !== null) { |
||
| 34 | // filePath = filePathTest.path |
||
| 35 | jsonPath = filePathTest.path |
||
| 36 | linkPath = filePathTest.abe_meta.link |
||
| 37 | } |
||
| 38 | |||
| 39 | if(jsonPath === null || !fileUtils.isFile(jsonPath)) { |
||
| 40 | res.status(404).send('Not found') |
||
| 41 | return |
||
| 42 | } |
||
| 43 | |||
| 44 | if(typeof json === 'undefined' || json === null) { |
||
| 45 | json = FileParser.getJson(jsonPath) |
||
| 46 | } |
||
| 47 | |||
| 48 | let meta = config.meta.name |
||
| 49 | |||
| 50 | var template = '' |
||
| 51 | if(typeof json[meta] !== 'undefined' && json[meta] !== null && json[meta] !== '' |
||
| 52 | && json[meta].template !== 'undefined' && json[meta].template !== null && json[meta].template !== '') { |
||
| 53 | template = json[meta].template |
||
| 54 | }else { |
||
| 55 | template = req.params[0] |
||
| 56 | } |
||
| 57 | var text = cmsTemplate.template.getTemplate(template) |
||
| 58 | |||
| 59 | if (!editor) { |
||
| 60 | |||
| 61 | cmsData.source.getDataList(fileUtils.removeLast(linkPath), text, json) |
||
| 62 | .then(() => { |
||
| 63 | var page = new Page(template, text, json, html) |
||
| 64 | res.set('Content-Type', 'text/html') |
||
| 65 | res.send(page.html) |
||
| 66 | }).catch(function(e) { |
||
| 67 | console.error(e) |
||
| 68 | }) |
||
| 69 | }else { |
||
| 70 | text = cmsData.source.removeDataList(text) |
||
| 71 | var page = new Page(template, text, json, html) |
||
| 72 | res.set('Content-Type', 'text/html') |
||
| 73 | res.send(page.html) |
||
| 74 | } |
||
| 75 | }else { |
||
| 76 | // not 404 page if tag abe image upload into each block |
||
| 77 | if(/upload%20image/g.test(req.url)) { |
||
| 78 | var b64str = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' |
||
| 79 | var img = new Buffer(b64str, 'base64') |
||
| 80 | |||
| 81 | res.writeHead(200, { |
||
| 82 | 'Content-Type': 'image/jpeg', |
||
| 83 | 'Content-Length': img.length |
||
| 84 | }) |
||
| 85 | res.end(img) |
||
| 86 | }else { |
||
| 87 | res.status(404).send('Not found') |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | export default page |